#include <stdio.h>
#include "common.h"
#include "network.h"
#include "dhstruct.h"
#include "dhavstream.h"
#include "dhpoe.h"

int GetCompleteData(DoMsgPacket_T *rcvPacket)
{
    int packetLen = 0;
	int countlen = 0;
    char *p = NULL;
    DVRIP_HEAD_T dvrs = {{0},};
    static  char buf[128 * 1024] = {0};
	static  char realBuf[32*1024] = {0};
	static  DoMsgPacket_T packet = {0};
	static  int bufflen = 0;
	
	memcpy(buf+bufflen,rcvPacket->recvMsg,rcvPacket->recvLen);
	bufflen += rcvPacket->recvLen;
	p = buf;
	
    while(p < (buf + bufflen))
	{
		DF_DEBUG("bfflen = %d",bufflen)
		if(*p!=0)
		{

			memset(&dvrs, 0, DVRIP_HEAD_T_SIZE);
	        memcpy(&dvrs, p, DVRIP_HEAD_T_SIZE);
	        packetLen = dvrs.data.dvrip_extlen + DVRIP_HEAD_T_SIZE;
			if(bufflen - countlen < packetLen)
			{
				DF_DEBUG("data is not complete");
				break;
			}
			memset(realBuf,0,packetLen+1);
			memcpy(realBuf, p, packetLen);
			memset(&packet,rcvPacket,sizeof(DoMsgPacket_T));
			packet.recvMsg = realBuf;
			packet.recvLen = packetLen;
			DHDoMsgRequest(&packet);
			if(packet.replyLen > 0){
				DF_DEBUG("packet.replyLen  = %d",packet.replyLen );	
				NetTcpSendMsg(packet.recvSocket,packet.replyMsg,packet.replyLen);
			}
			countlen += packetLen;
			bufflen -= packetLen;
			p += packetLen;
		}
		else
		{
			DF_DEBUG("why?");
			p++;
			bufflen--;
		}
	}
	memcpy(buf,p,bufflen);
	memset(buf+bufflen,0,sizeof(buf) - bufflen);
	return 0;
}

int DHRequst(NetCliInfo_T *cli)
{
	DoMsgPacket_T packet = {0};
	memset(&packet,0,sizeof(DoMsgPacket_T));
	if(	0 == strlen(cli->fromIP) || 
		0 >= cli->recvLen || 
		0 >= cli->fromPort || 
		0 >= cli->recvSocket){
		//free(cli);
		DF_DEBUG("DHRequst param error");
		return KEY_FALSE;
	}
	DF_DEBUG("ip = %s",cli->fromIP);
	///DF_DEBUG("port = %d",cli->fromPort);
	//DF_DEBUG("msg = %s",cli->recvmsg);
	//_DEBUG("socket = %d",cli->recvSocket);	
	DF_DEBUG("recvLen = %d",cli->recvLen);
	
	sprintf(packet.cliIP,cli->fromIP);
	packet.cliPort = cli->fromPort;
	packet.recvLen = cli->recvLen;
	packet.recvMsg = cli->recvmsg;
	packet.recvSocket = cli->recvSocket;
	//GetCompleteData(&packet);
	DHDoMsgRequest(&packet);
	if(packet.replyLen > 0){
		DF_DEBUG("packet.replyLen  = %d",packet.replyLen );	
		NetTcpSendMsg(packet.recvSocket,packet.replyMsg,packet.replyLen);
	}
	//free(cli);
	return KEY_TRUE;
}

int DHChecked(NetCliInfo_T *cli)
{

	return KEY_TRUE;
}


int DHCloseSocketCb(int sockfd)
{
	DF_DEBUG("Close Socked = %d",sockfd);
	return KEY_TRUE;
}



int DHDiscorverRecv(NetCliInfo_T *cli)
{
	DoMsgPacket_T packet = {0};
	if(	0 == strlen(cli->fromIP) || 
		0 >= cli->recvLen || 
		0 >= cli->fromPort){
		//free(cli);
		DF_DEBUG("DHDiscorverRecv param error");
		return KEY_FALSE;
	}
//	DF_DEBUG("ip = %s",cli->fromIP);
//	DF_DEBUG("port = %d",cli->fromPort);
//	DF_DEBUG("msg = %s",cli->recvmsg);
//	DF_DEBUG("socket = %d",cli->recvSocket);	
//	DF_DEBUG("recvLen = %d",cli->recvLen);

	sprintf(packet.cliIP,cli->fromIP);
	packet.cliPort = cli->fromPort;
	packet.recvLen = cli->recvLen;
	packet.recvMsg = cli->recvmsg;
	packet.recvSocket = cli->recvSocket;
	memset(packet.replyMsg,0,sizeof(packet.replyMsg));
	
	//ParaOnvifMsg(&obj);
//	DF_DEBUG("NetTcpSendMsg");
	//NetUdpSendMsg(cli->fromIP,cli->fromPort,obj.buf,strlen(obj.buf));
	//DF_DEBUG("buf = %s",obj.buf);
	//DF_DEBUG("buflen = %d",strlen(obj.buf));
	if(KEY_TRUE == DHDoDiscoverRequest(&packet)){
		DF_DEBUG("reply len = %d",packet.replyLen);
		NetUdpSendMsgBySocket(packet.recvSocket,"255.255.255.255",5050,packet.replyMsg,packet.replyLen);
	}
	//free(cli);
	return KEY_TRUE;
}



NetWorkSerHandle_T *GetDHSerNetHandle()
{
	static  NetWorkSerHandle_T g_DHSerNetHandle;
	return  &g_DHSerNetHandle;
}

NetWorkSerHandle_T *GetDHBroadCastSerHandle()
{
	static NetWorkSerHandle_T g_DHBroadcastSerHandle;
	return  &g_DHBroadcastSerHandle;
}

int DHSerStart()
{
	ProtocolAPIHandle_T *handle = GetProtocolAPIHandle();
	NetWorkSerHandle_T *netHandle = GetDHSerNetHandle();
	NetWorkSerHandle_T *BroadcastHandle = GetDHBroadCastSerHandle();
	NetProtocolInfo_T info = {0};
	APIISNULL(handle->pGetNetProtocolInfo);
	DHSessionInit();
	handle->pGetNetProtocolInfo(&info);
	netHandle->readcb = DHRequst;
	netHandle->checkendcb = DHChecked;
	netHandle->closeCb = DHCloseSocketCb;
	netHandle->serverport = info.DahuaPort;
	
	BroadcastHandle->broadcastPort = 5050;
	BroadcastHandle->readcb = DHDiscorverRecv;
	NetCreateTcpSer(netHandle);
	NetCreateUdpBroadcast(BroadcastHandle);
	RegisterAPISendVideoStream(DHSendVideoStream);
	DHPPPOESearchStart();
	DF_DEBUG("DH Server Start");
	return 0;
}

int DHServerStop()
{
	NetWorkSerHandle_T *netHandle = GetDHSerNetHandle();
	NetWorkSerHandle_T *BroadcastHandle = GetDHBroadCastSerHandle();
	NetCloseSerSocket(netHandle);
	NetCloseSerSocket(BroadcastHandle);
	DF_DEBUG("DH Server Stop");
	return 0;
}



